package com.sky.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //怎么获取已经在数据库中有的购物车数据？->userId + dishId or SetmealId 来唯一确定是哪个数据

        //ShoppingCart shoppingCart=查数据库 -> null? -> 如果不是null直接用就行，是null那就new一个新的
        //获取请求头 todo 把获取userId的业务放进拦截器存入ThreadLocal
        String token = request.getHeader("authentication");
        log.info("token:{}", token);
        Object id = StpUtil.getLoginIdByToken(token);
        log.info("id:{}", id);
        Long userId = Long.parseLong(id + "");

        ShoppingCart shoppingCart = null;

        //查菜品？查套餐？
        if (shoppingCartDTO.getDishId() != null) {
            //查菜品
            Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());

            ShoppingCart paras = new ShoppingCart();
            paras.setUserId(userId);
            paras.setDishId(dish.getId());

            shoppingCart = shoppingCartMapper.getByDishIdAndUserId(paras);

            if (shoppingCart == null) {
                shoppingCart = new ShoppingCart();
                shoppingCart.setUserId(userId);
            }

            //数据填充
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setDishId(dish.getId());
            //商品口味可以有很多，但是购买的时候一定是只能买一个口味了
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            //算钱 单品的总价=单品单价*数量
            //数量问题怎么解决？

            long count = shoppingCartMapper.countByDishIdAndUserId(paras);
            if (count == 0) {
                //第一次加入购物车
                shoppingCart.setNumber(1);
                BigDecimal amount = dish.getPrice().multiply(BigDecimal.valueOf(shoppingCart.getNumber()));
                shoppingCart.setAmount(amount);
                shoppingCartMapper.add(shoppingCart);
            } else {
                //第n次购买，n>=2
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                BigDecimal amount = dish.getPrice().multiply(BigDecimal.valueOf(shoppingCart.getNumber()));
                shoppingCart.setAmount(amount);
                shoppingCartMapper.update(shoppingCart);
            }


        }
//todo 套餐业务实现

//        if(shoppingCartDTO.getSetmealId()!=null){
//            //查套餐？
//            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
//            shoppingCart.setName(setmeal.getName());
//            shoppingCart.setImage(setmeal.getImage());
//            shoppingCart.setId(setmeal.getId());
//            //算钱 套餐的总价=套餐单价*数量
//            BigDecimal amount=setmeal.getPrice().multiply(BigDecimal.valueOf(shoppingCart.getNumber()));
//            shoppingCart.setAmount(amount);
//        }


    }

    @Override
    public List<ShoppingCart> list() {
        //获取请求头
        String token = request.getHeader("authentication");
        log.info("token:{}", token);
        Object id = StpUtil.getLoginIdByToken(token);
        log.info("id:{}", id);
        return shoppingCartMapper.list(Long.parseLong(id + ""));
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //获取请求头
        //todo 需要自己实现套餐
        String token = request.getHeader("authentication");
        log.info("token:{}", token);
        Object id = StpUtil.getLoginIdByToken(token);
        log.info("id:{}", id);
        Long userId = Long.parseLong(id + "");

        ShoppingCart paras = new ShoppingCart();
        paras.setUserId(userId);
        paras.setDishId(shoppingCartDTO.getDishId());
        ShoppingCart shoppingCart = shoppingCartMapper.getByDishIdAndUserId(paras);
        //减数量 -> 减价格
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);

        //todo 考虑特例，当数量为0时，这条记录就需要删除了
        if(shoppingCart.getNumber()==0){
            shoppingCartMapper.deleteById(shoppingCart.getId());
            //因为记录已经被删除了，下面的计算和更新都不必执行了
            return;
        }

        Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
        BigDecimal amount = dish.getPrice().multiply(BigDecimal.valueOf(shoppingCart.getNumber()));
        shoppingCart.setAmount(amount);

        shoppingCartMapper.update(shoppingCart);
    }

    @Override
    public void clean() {
        String token = request.getHeader("authentication");
        log.info("token:{}", token);
        Object id = StpUtil.getLoginIdByToken(token);
        log.info("id:{}", id);
        Long userId = Long.parseLong(id + "");

        shoppingCartMapper.deleteByUserId(userId);
    }
}
